--[[
 * Copyright (C) 2015 - 2018 Ricky K. Thomson
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * u should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 --]]
 
--碰撞后会死掉

local materials = class("Materials")



-- table.insert(editor.entities, {"death", "material"})
function materials:ctor()
	self.death = love.graphics.newImage("res/images/materials/death.png")
	self.death:setWrap("repeat","repeat")
end

function materials:add(x,y,w,h,t)
	table.insert(world.entities.material, {		
		--position
		x = x or 0,
		y = y or 0,
		w = w or 10,
		h = h or 10,
		quad = love.graphics.newQuad( 0,0, w,h, self.death:getDimensions() ),
		--properties
		group = "material",
		type = t
	})
end


function materials:update(dt)
	if mode == "editing" then return end
	local player = global.player
	for _, mat in ipairs(world.entities.material) do
		if world:inview(mat) then
			if player:check(mat) then
				if mat.type == "death" then
					player.y = mat.y-player.h
					player:die("death material @ x:".. math.floor(player.x) .. " y:"..math.floor(player.y))
				end
			end
		end
	end
end


function materials:draw()
	if editing or debug then
	
		for i, mat in ipairs(world.entities.material) do
			if world:inview(mat) then
				if mat.type == "death" then
					
					love.graphics.setColor(0,0,0,0.39)
					love.graphics.rectangle("fill",mat.x,mat.y,mat.w,mat.h)
					love.graphics.setColor(0,0,0,1)
					love.graphics.rectangle("line",mat.x,mat.y,mat.w,mat.h)
					love.graphics.setColor(1,1,1,0.39)
					love.graphics.draw(self.death, mat.quad, mat.x,mat.y)
				end
			
			end
		end
				

	end
end



return materials